package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 老杨
 * @version 1.0
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {


    @Autowired
    ShoppingCartMapper shoppingCartMapper;
    @Autowired
    DishMapper dishMapper;
    @Autowired
    SetmealMapper setmealMapper;
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        //先构建 购物车对象
            //1.拿到当前 用户id
        Long currentId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(currentId)
                .dishId(shoppingCartDTO.getDishId())
                .dishFlavor(shoppingCartDTO.getDishFlavor())
                .setmealId(shoppingCartDTO.getSetmealId()).build();
        //添加菜品 或 套餐到购物车前，判断是否已经 添加过一样的
        //一样 就数量 加1 即可
      List< ShoppingCart> list = shoppingCartMapper.select(shoppingCart);
        if (list != null && list.size()>0){
            //则进行 修改操作，在原有 num 基础上加1
            ShoppingCart select = list.get(0);
            select.setNumber(select.getNumber()+1);
            shoppingCartMapper.update(select);
        }else {
            //没有 就完善购物车对象数据，然后添加 到购物车
                //先确定添加的是套餐还是 菜品
            Long dishId = shoppingCart.getDishId();
            if (dishId != null){
                //添加 的是菜品
                Dish dish = dishMapper.getById(shoppingCart.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }else {
                //否则就是套餐
                Setmeal setmeal = setmealMapper.select(shoppingCart.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
        //插入数据
            shoppingCartMapper.insert(shoppingCart);
        }





    }

    @Override
    public  List<ShoppingCart> list() {
        //只能获取 用户自己的购物车
        Long currentId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(currentId)
                .build();
        List<ShoppingCart> list = shoppingCartMapper.select(shoppingCart);
        return list;
    }

    @Override
    public void clean() {
        Long currentId = BaseContext.getCurrentId();
        //根据 用户 id删除对应购物车
        shoppingCartMapper.delete2(currentId);
    }

    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        //拷贝数据
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //查询 数据
        List<ShoppingCart> select = shoppingCartMapper.select(shoppingCart);
        //判断 数据是否存在
        if (select == null || select.size() < 0){
            throw new RuntimeException("要减少或删除 的数据为null");
        }
        ShoppingCart shoppingCart1 = select.get(0);
        //判断 数量是否 大于 1，大于1 ，就减一，小于等于1 就删除这条记录
        if (shoppingCart1.getNumber() > 1){
            //数量减1
            //跟新操作
            shoppingCart1.setNumber(shoppingCart1.getNumber() -1);
            shoppingCartMapper.update(shoppingCart1);
        }else {
            //删除 该条记录
            List<Long> longs = new ArrayList<>();
            longs.add(shoppingCart1.getId());
            shoppingCartMapper.delete(longs);
        }


    }
}
